﻿using Sundry.App_Start.Common;
using Sundry.BLL;
using Sundry.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sundry.Areas.Admin.Controllers
{
    [Sundry.App_Start.AdminLoginAuthor]
    public class BrandController : Controller
    {
        //
        // GET: /Admin/Brand/

        public ActionResult BrandList() {
            return View();
        }
        /// <summary>
        /// 显示品牌列表
        /// </summary>
        /// <param name="param">品牌列表分页相关信息</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult BrandList(Sundry.Areas.Admin.Models.PageBrandModel param) {
            int allCount = 0;
            var log = EntryService.GetServiceEntrace().GetBrandService.LoadBradnList(param.BrandName, param.iDisplayStart, param.iDisplayLength, out allCount).ToList();
            return Json(new {
                sEcho = param.sEcho,
                iTotalRecords = allCount,
                iTotalDisplayRecords = allCount,
                aaData = log
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddBrand(Sundry.Areas.Admin.Models.BrandModel model) {
            //判断品牌名称是否存在
            if (EntryService.GetServiceEntrace().GetBrandService.SelectFirstBrand(m => m.Name == model.BrandName) != null) {
                return Json(new {
                    state = "fail",
                    message = "品牌名称已存在"
                });
            }
            else {
                if (ModelState.IsValid) {
                    //第一步：先根据64位字符串生成图片
                    Random rn = new Random();
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmsss") + rn.Next(100000, 900000).ToString() + "." + model.Extension;
                    if (model.Logo.Split(',').Length > 1) {
                        model.Logo = model.Logo.Split(',')[1];
                    }
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssss") + new Random().Next(100000, 999999).ToString() + ".png" ;
                    string result = Sundry.App_Start.Common.UploadImgHelper.SaveBrandImg(model.Logo, filename, UserCookie.GetAdminId());
                    if (result == "-1") {
                        return Json(new {
                            state = "fail",
                            message = "上传失败"
                        });
                    }
                    else if (result == "-2") {
                        return Json(new {
                            state = "fail",
                            message = "图片格式不对"
                        });
                    }
                    else if (result == "-3") {
                        return Json(new {
                            state = "fail",
                            message = "上传文件超出可上传大小"
                        });
                    }
                    else {
                        if (EntryService.GetServiceEntrace().GetBrandService.AddEntity(new Model.Brand() {
                            CreateTime = DateTime.Now,
                            Name = model.BrandName,
                            Intro = model.Intro,
                            IsRec = model.IsRec,
                            IsShow = model.IsShow,
                            Logo = result,
                            OrderNum = model.OrderNum
                        }) > 0) {
                            PubliFunction.InsertOperation(1, "添加品牌", string.Format("添加品牌{0}成功", model.BrandName));
                            return Json(new {
                                state = "success",
                                message = "添加品牌成功"
                            });
                        }
                        else {
                            PubliFunction.InsertOperation(1, "添加品牌", string.Format("添加品牌{0}失败", model.BrandName));
                            return Json(new {
                                state = "error",
                                message = "添加品牌失败"
                            });
                        }
                    }
                }
                else {
                    return Json(new {
                        state = "fail",
                        message = "输入的信息验证未通过"
                    });
                }
            }
        }

        /// <summary>
        /// 判断品牌名称是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ExistBrand(Sundry.Areas.Admin.Models.BrandModel model) {
            //判断品牌名称是否存在
            if (EntryService.GetServiceEntrace().GetBrandService.SelectFirstBrand(m => m.Name == model.BrandName) != null) {
                return Json(new {
                    state = "fail",
                    message = "品牌名称已存在"
                });
            }
            else {
                return Json(new {
                    state = "success",
                    message = "该品牌可以添加"
                });
            }
        }

        /// <summary>
        /// 删除单个品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteBrand(Sundry.Areas.Admin.Models.BrandIdModel model) {
            if (EntryService.GetServiceEntrace().GetBrandService.DeleteEntity(new Sundry.Model.Brand() {
                Id = model.Id
            })) {
                return Json(
                    new {
                        state = "success",
                        message = "删除成功"
                    }
                    );
            }
            else {
                return Json(
                       new {
                           state = "fail",
                           message = "删除失败"
                       }
                       );
            }
        }
        /// <summary>
        /// 根据品牌Id获取品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetBrandModel(Sundry.Areas.Admin.Models.BrandIdModel model) {
            Model.Brand brand = EntryService.GetServiceEntrace().GetBrandService.SelectFirstBrand(m => m.Id == model.Id);
            if (brand != null) {
                return Json(new {
                    state = "success",
                    Brand = brand
                });
            }
            else {
                return Json(new {
                    state = "fail",
                    message = "改品牌不存在"
                });
            }
        }
    }
}
